import pygame
import time
import random

# 初始化 pygame
pygame.init()

# 定义颜色
white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)

# 设置游戏窗口
width = 600
height = 400
gameDisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption('贪吃蛇游戏')

# 定义蛇的大小
block_size = 10

# 设置游戏时钟
clock = pygame.time.Clock()

# 设置蛇的速度
snake_speed = 15

# 定义字体（指定中文字体）
font_path = "C:/Windows/Fonts/simhei.ttf"  # Windows系统的黑体路径，可以根据自己的系统进行调整
font_style = pygame.font.Font(font_path, 25)  # 使用黑体字体，大小为25
score_font = pygame.font.SysFont("comicsansms", 35)

# 显示得分
def Your_score(score):
    value = score_font.render("score: " + str(score), True, black)
    gameDisplay.blit(value, [0, 0])

# 绘制蛇
def our_snake(block_size, snake_list):
    for x in snake_list:
        pygame.draw.rect(gameDisplay, green, [x[0], x[1], block_size, block_size])

# 显示消息
def message(msg, color):
    mesg = font_style.render(msg, True, color)
    gameDisplay.blit(mesg, [width / 6, height / 3])

# 游戏主循环
def gameLoop():
    game_over = False
    game_close = False

    # 初始蛇的位置
    x1 = width / 2
    y1 = height / 2

    # 蛇的移动速度
    x1_change = 0
    y1_change = 0

    # 蛇的身体
    snake_List = []
    Length_of_snake = 1

    # 随机生成食物的位置
    foodx = round(random.randrange(0, width - block_size) / 10.0) * 10.0
    foody = round(random.randrange(0, height - block_size) / 10.0) * 10.0

    while not game_over:

        while game_close:
            gameDisplay.fill(blue)
            message("游戏结束! 按 ctrl + Q 退出或 ctrl + C 重新开始", red)
            Your_score(Length_of_snake - 1)
            pygame.display.update()

            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        game_over = True
                        game_close = False
                    if event.key == pygame.K_c:
                        gameLoop()

        # 获取用户按键输入
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x1_change = -block_size
                    y1_change = 0
                elif event.key == pygame.K_RIGHT:
                    x1_change = block_size
                    y1_change = 0
                elif event.key == pygame.K_UP:
                    y1_change = -block_size
                    x1_change = 0
                elif event.key == pygame.K_DOWN:
                    y1_change = block_size
                    x1_change = 0

        # 游戏边界碰撞判断
        if x1 >= width or x1 < 0 or y1 >= height or y1 < 0:
            game_close = True

        # 更新蛇的位置
        x1 += x1_change
        y1 += y1_change
        gameDisplay.fill(blue)

        # 绘制食物
        pygame.draw.rect(gameDisplay, yellow, [foodx, foody, block_size, block_size])

        # 更新蛇的身体
        snake_Head = []
        snake_Head.append(x1)
        snake_Head.append(y1)
        snake_List.append(snake_Head)
        if len(snake_List) > Length_of_snake:
            del snake_List[0]

        # 判断蛇是否碰到自己
        for x in snake_List[:-1]:
            if x == snake_Head:
                game_close = True

        # 绘制蛇
        our_snake(block_size, snake_List)

        # 显示得分
        Your_score(Length_of_snake - 1)

        pygame.display.update()

        # 判断蛇是否吃到食物
        if x1 == foodx and y1 == foody:
            foodx = round(random.randrange(0, width - block_size) / 10.0) * 10.0
            foody = round(random.randrange(0, height - block_size) / 10.0) * 10.0
            Length_of_snake += 1

        # 设置游戏的帧率
        clock.tick(snake_speed)

    pygame.quit()
    quit()

# 启动游戏
gameLoop()
